<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/14
 * Time: 5:02
 */

namespace app\api\controller\v1\product;
use app\api\controller\v1\Init;

class ProductOrder extends Init
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('product/product_order', 'service');
    }

    /**
     * 获取订单列表
     * @return \think\Response
     */
    public function lists() {
        $maps = [];
        $maps['uid'] = $this->user['id'];
        if(isset($this->params['stage'])) {
            $maps['stage'] = (int) $this->params['stage'];
        }

        if(isset($this->params['status'])) {
            $maps['status'] = (int) $this->params['status'];
        }
        $order = $this->params['order'];
        $extra = $this->params['extra'] && !is_array($this->params['extra']) ? explode(",", $this->params['extra']) : [];
        $adds = [];
        $attrs = $this->params['attrs'] ? explode(",", $this->params['attrs']) : [];
        $result = $this->model->lists($maps, $this->limit, $this->page, $order, true, $extra, $adds, $attrs);
        return $this->response(lang('Operation completed'), $result);
    }

    /**
     * 订单详情
     * @return \think\Response
     */
    public function item() {
        $maps = [];
        $maps['sn'] = $this->params['sn'];
        if($this->params['uid']) {
            $maps['uid'] = $this->params['uid'];
        }
        $result = $this->model->where($maps)->find();
        if($this->params['attrs']) {
            $attrs = explode(",",$this->params['attrs']);
            array_map(function($attr) use (&$result) {
                $attr = trim($attr);
                return $result->$attr = $result->getAttr($attr);
            }, $attrs);
        }
        if($this->params['extra']) {
            $extra = (is_string($this->params['extra'])) ? explode(",", $this->params['extra']) : $this->params['extra'];
            array_map(function($e) use (&$result) {
               $e = trim($e);
               return $result->$e;
            }, $extra);
        }
        return $this->response('', $result);
    }

    public function invite() {
        $result = $this->model->invite(
            $this->user['id'],
            $this->params['realname'],
            $this->params['mobile'],
            $this->params['profile_template_id']
        );
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }

    /**
     * 创建产品订单
     * @return \think\Response
     */
    public function create() {
        $result = $this->model->apply(
            $this->user['id'],
            $this->params['product_id'],
            $this->params['question'],
            $this->params['realname'],
            $this->params['mobile']
        );
        if(!$result) {
            return $this->response($this->model->getError(),[], -20002);
        }
        return $this->response('操作成功', $result);
    }


    /**
     * 客户经理首次联系
     * @return \think\Response
     */
    public function contact() {
        $result = $this->model->contact($this->params['sn'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }


    /**
     * 客户确认联系
     * @return \think\Response
     */
    public function confirm() {
        $result = $this->model->confirm($this->params['sn'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }

    /**
     * 咨询投诉
     * @return \think\Response
     */
    public function complain() {
        $result = $this->model->complain($this->params['sn'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }


    /**
     * 客户经理推送清单
     * @return \think\Response
     */
    public function push() {
        $result = $this->model->push($this->params['sn'], $this->params['profile'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }


    /**
     * 客户准备资料清单
     * @return \think\Response
     */
    public function profile() {
        $result = $this->model->profile($this->params['sn'], $this->params['profile']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('', $result);
    }

    /**
     * 客户经理实调签单
     */
    public function research() {
        $result = $this->model->research($this->params['sn'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('', $result);
    }


    /**
     * 客户经理上报
     * @return \think\Response
     */
    public function submit() {
        $result = $this->model->submit($this->params['sn'], $this->user['id']);
        if(!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }

    /**
     * 客户经理审批贷款申请
     * @return \think\Response
     */
    public function review()
    {
        $force = isset($this->params['force']) && $this->params['force'] == 1 ? true : false;

        $result = $this->model->review($this->params['sn'], $this->params['stage'], $this->user['id'], $force);
        if (!$result) {
            return $this->response($this->model->getError(), [], -20002);
        }
        return $this->response('操作成功', $result);
    }
}